package com.zhc.dejingapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhc.dejingapi.common.result.Result;
import com.zhc.dejingapi.entity.QingPersonEntity;
import com.zhc.dejingapi.entity.QingProcessplanEntity;
import com.zhc.dejingapi.entity.QingWithdrawalofpipedetailEntity;
import com.zhc.dejingapi.entity.QingWithdrawalofpipereportEntity;
import com.zhc.dejingapi.entity.query.CodeQuery;
import com.zhc.dejingapi.entity.vo.Code2Vo;
import com.zhc.dejingapi.entity.vo.QingInjectAndSendWorkVo;
import com.zhc.dejingapi.mapper.QingPersonMapper;
import com.zhc.dejingapi.mapper.QingProcessplanMapper;
import com.zhc.dejingapi.mapper.QingWithdrawalofpipedetailMapper;
import com.zhc.dejingapi.mapper.QingWithdrawalofpipereportMapper;
import com.zhc.dejingapi.service.QingWithdrawalofpipereportService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 注塑报工表 服务实现类
 * </p>
 *
 * @author zhc
 * @since 2024-10-18
 */
@Service
public class QingWithdrawalofpipereportServiceImpl extends ServiceImpl<QingWithdrawalofpipereportMapper, QingWithdrawalofpipereportEntity> implements QingWithdrawalofpipereportService {

    @Resource
    private QingWithdrawalofpipereportMapper qingWithdrawalofpipereportMapper;
    @Resource
    private QingPersonMapper qingPersonMapper;

    @Resource
    private QingWithdrawalofpipedetailMapper qingWithdrawalofpipedetailMapper;
    @Resource
    private QingProcessplanMapper qingProcessplanMapper;
    @Override
    public Result reportWithdrawalofpipeJob(QingInjectAndSendWorkVo qingInjectAndSendWorkVo) {
        QingWithdrawalofpipereportEntity qingWithdrawalofpipereportEntity = new QingWithdrawalofpipereportEntity();
        BeanUtil.copyProperties(qingInjectAndSendWorkVo, qingWithdrawalofpipereportEntity);
        QueryWrapper<QingPersonEntity> qWPerson=new QueryWrapper<>();
        qWPerson.eq("psnCode",qingInjectAndSendWorkVo.getPsnCode());
        QingPersonEntity qingPersonEntity = qingPersonMapper.selectOne(qWPerson);
        qingWithdrawalofpipereportEntity.setPsnName(qingPersonEntity.getPsnName());
        int insert = qingWithdrawalofpipereportMapper.insert(qingWithdrawalofpipereportEntity);

        //TODO
        // 箱码
        Date date = DateUtil.date();
        String dateformat = DateUtil.format(date, "yyyyMMdd");
        String processPlanCode = qingInjectAndSendWorkVo.getProcessPlanCode();
        String barCode = processPlanCode +"-"+ dateformat;
        long barCodeNum = qingWithdrawalofpipedetailMapper.getBarCodeNum(barCode);

        String barcodeStr = "";

        if (barCodeNum < 1) {

            if(qingWithdrawalofpipereportEntity.getModQty()==0){
                for(int i=1;i<=qingWithdrawalofpipereportEntity.getProNum();i++) {
                    QingWithdrawalofpipedetailEntity qingWithdrawalofpipedetailEntity = new QingWithdrawalofpipedetailEntity();
                    String num = String.format("%03d", i);
                    barcodeStr = barCode +"-"+ num;
                    qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                    qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                    qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getQtyPerCarton());
                    qingWithdrawalofpipedetailEntity.setINum(1);
                    qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                }
            }else{
                for(int i=1;i<=qingWithdrawalofpipereportEntity.getProNum();i++) {
                    QingWithdrawalofpipedetailEntity qingWithdrawalofpipedetailEntity = new QingWithdrawalofpipedetailEntity();
                    String num = String.format("%03d", i);
                    barcodeStr = barCode +"-"+ num;
                    if(i<qingWithdrawalofpipereportEntity.getProNum()){
                        qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                        qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                        qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getQtyPerCarton());
                        qingWithdrawalofpipedetailEntity.setINum(1);
                        qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                    }else {
                        qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                        qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                        qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getModQty());
                        qingWithdrawalofpipedetailEntity.setINum(1);
                        qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                    }

                }

            }



        } else {

            if(qingWithdrawalofpipereportEntity.getModQty()==0){
                for(int i=1;i<=qingWithdrawalofpipereportEntity.getProNum();i++) {
                    QingWithdrawalofpipedetailEntity qingWithdrawalofpipedetailEntity = new QingWithdrawalofpipedetailEntity();
                    String num = String.format("%03d", barCodeNum + i);
                    barcodeStr = barCode +"-"+ num;
                    qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                    qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                    qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getQtyPerCarton());
                    qingWithdrawalofpipedetailEntity.setINum(1);
                    qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                }
            }else{
                for(int i=1;i<=qingWithdrawalofpipereportEntity.getProNum();i++) {
                    QingWithdrawalofpipedetailEntity qingWithdrawalofpipedetailEntity = new QingWithdrawalofpipedetailEntity();
                    String num = String.format("%03d", barCodeNum + i);
                    barcodeStr = barCode +"-"+ num;
                    if(i<qingWithdrawalofpipereportEntity.getProNum()){
                        qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                        qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                        qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getQtyPerCarton());
                        qingWithdrawalofpipedetailEntity.setINum(1);
                        qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                    }else {
                        qingWithdrawalofpipedetailEntity.setMainId(qingWithdrawalofpipereportEntity.getMainId());
                        qingWithdrawalofpipedetailEntity.setBarcodeStr(barcodeStr);
                        qingWithdrawalofpipedetailEntity.setIQty(qingWithdrawalofpipereportEntity.getModQty());
                        qingWithdrawalofpipedetailEntity.setINum(1);
                        qingWithdrawalofpipedetailMapper.insert(qingWithdrawalofpipedetailEntity);
                    }

                }

            }

        }


        //TODO
        // 注塑派工单回写报工数量
//        QueryWrapper<QingInjectionmoldsendworkdetailEntity> queryWrapper=new QueryWrapper<>();
//
//        queryWrapper.eq("zs_send_work_code",qingInjectreportEntity.getZsSendWorkCode());
//        QingInjectionmoldsendworkdetailEntity qingInSend = qingInjectionmoldsendworkdetailMapper.selectOne(queryWrapper);
//        int totalReportNum =0;
//        if(qingInSend.getTotalReportNum()!=null){
//            totalReportNum=qingInSend.getTotalReportNum();
//        }
//
//        qingInSend.setTotalReportNum(totalReportNum+qingInjectreportEntity.getQualifyQty());
//        qingInjectionmoldsendworkdetailMapper.update(qingInSend,queryWrapper);

        //TODO
        // 工序计划回写报工数量

        QueryWrapper<QingProcessplanEntity> qW=new QueryWrapper<>();

        qW.eq("process_plan_code",qingWithdrawalofpipereportEntity.getProcessPlanCode());
        QingProcessplanEntity qingProcessplan = qingProcessplanMapper.selectOne(qW);
        int totalReportNum1 =0;
        if(qingProcessplan.getTotalReportNum()!=null){
            totalReportNum1=qingProcessplan.getTotalReportNum();
        }
        qingProcessplan.setTotalReportNum(totalReportNum1+qingWithdrawalofpipereportEntity.getQualifyQty());
        qingProcessplanMapper.update(qingProcessplan,qW);
        if(insert>0){
            return Result.ok(qingWithdrawalofpipereportEntity.getMainId());
        }else {
            return Result.fail(null);
        }
    }

    @Override
    public PageInfo<Code2Vo> getCodeList(CodeQuery codeQuery) {
        int current=codeQuery.getPageCurrent();
        int size=codeQuery.getPageSize();

        //获取第1页，10条内容，默认查询总数count
        PageHelper.startPage(current, size);
        //只有紧跟着PageHelper.startPage()的sql起了作用
        List<Code2Vo> codeList = qingWithdrawalofpipereportMapper.getCodeList(codeQuery);
        //用PageInfo对结果进行包装
        PageInfo<Code2Vo> page = new PageInfo<>(codeList);

        return page;
    }
}
